{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CrewAI:Tasks\n",
    "\n",
    "Task是Agent完成的特定作业。\n",
    "    \n",
    "Task提供了执行的所有必要详细信息，例如描述，负责的agent，所需tools等，以适用从简单到复杂的各种类型的任务。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'researcher' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 12\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mcrewai\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m Task\n\u001b[0;32m      3\u001b[0m research_task \u001b[38;5;241m=\u001b[39m Task(\n\u001b[0;32m      4\u001b[0m     description\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m\n\u001b[0;32m      5\u001b[0m \u001b[38;5;124m        Conduct a thorough research about AI Agents.\u001b[39m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;124m        Make sure you find any interesting and relevant information given\u001b[39m\n\u001b[0;32m      7\u001b[0m \u001b[38;5;124m        the current year is 2024.\u001b[39m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;124m    \u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m,\n\u001b[0;32m      9\u001b[0m     expected_output\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m\n\u001b[0;32m     10\u001b[0m \u001b[38;5;124m        A list with 10 bullet points of the most relevant information about AI Agents\u001b[39m\n\u001b[0;32m     11\u001b[0m \u001b[38;5;124m    \u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m,\n\u001b[1;32m---> 12\u001b[0m     agent\u001b[38;5;241m=\u001b[39m\u001b[43mresearcher\u001b[49m\n\u001b[0;32m     13\u001b[0m )\n\u001b[0;32m     15\u001b[0m reporting_task \u001b[38;5;241m=\u001b[39m Task(\n\u001b[0;32m     16\u001b[0m     description\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m\n\u001b[0;32m     17\u001b[0m \u001b[38;5;124m        Review the context you got and expand each topic into a full section for a report.\u001b[39m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     25\u001b[0m     output_file\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mreport.md\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m     26\u001b[0m )\n",
      "\u001b[1;31mNameError\u001b[0m: name 'researcher' is not defined"
     ]
    }
   ],
   "source": [
    "from crewai import Task\n",
    "\n",
    "research_task = Task(\n",
    "    description=\"\"\"\n",
    "        Conduct a thorough research about AI Agents.\n",
    "        Make sure you find any interesting and relevant information given\n",
    "        the current year is 2024.\n",
    "    \"\"\",                            # 关于任务需要什么的清晰，简洁的陈述。\n",
    "    expected_output=\"\"\"\n",
    "        A list with 10 bullet points of the most relevant information about AI Agents\n",
    "    \"\"\",                            # 任务的预期输出\n",
    "    name = \"task name\",             # 任务的名字\n",
    "    tools = [toolname],             # 任务可使用的工具\n",
    "    context = [\"Task_a\",\"Task_b\"],  # Other tasks whose outputs will be used as context for this task.\n",
    "    async_execution\t= False,        # 是否异步执行，默认False\n",
    "    human_input = False,            # 该任务的输出是否需要人工审核环节，默认False\n",
    "    config = {\"max_tokens\": 512},   # 任务的配置，如max_tokens等\n",
    "    output_file\t= \"research_output.txt\",    # 输出文件的路径\n",
    "    output_json\t= Type[BaseModel],  # 是否输出为特定格式，A Pydantic model to structure the JSON output.\n",
    "    output_pydantic = Type[BaseModel],  # A Pydantic model for task output.\n",
    "    callback = func_a,              # 回调函数，当任务执行完成后，会调用这个函数   \n",
    "    agent=researcher                # 负责执行任务的代理\n",
    ")\n",
    "\n",
    "# task必要的参数只有三个。\n",
    "reporting_task = Task(\n",
    "    description=\"\"\"\n",
    "        Review the context you got and expand each topic into a full section for a report.\n",
    "        Make sure the report is detailed and contains any and all relevant information.\n",
    "    \"\"\",\n",
    "    expected_output=\"\"\"\n",
    "        A fully fledge reports with the mains topics, each with a full section of information.\n",
    "        Formatted as markdown without '```'\n",
    "    \"\"\",\n",
    "    agent=reporting_analyst,\n",
    ")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
